package org.pzy.java.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Iterator;

/**
 * @author pzy
 */
public class Demo01 {
    public static void main(String[] args) throws JsonProcessingException {
        String s = "{\n" +
                "  \"1\": { \"m_src\": \"/upload/AppLoad/58622b25d41c95631.jpg\", \"type\": \"img_1\" },\n" +
                "  \"2\": { \"m_src\": \"/upload/AppLoad/58622b28cded19231.jpg\", \"type\": \"img_2\" },\n" +
                "  \"3\": { \"m_src\": \"/upload/AppLoad/58622b2bc7b881635.jpg\", \"type\": \"img_3\" },\n" +
                "  \"4\": { \"m_src\": \"/upload/AppLoad/58622b359e7976203.jpg\", \"type\": \"img_4\" },\n" +
                "  \"5\": { \"m_src\": \"/upload/AppLoad/58622b3b346c19859.jpg\", \"type\": \"img_5\" },\n" +
                "  \"24\": { \"m_src\": \"/upload/AppLoad/58622b3f0bc9c1583.jpg\", \"type\": \"img_24\" },\n" +
                "  \"25\": { \"m_src\": \"/upload/AppLoad/58622b42b00819128.jpg\", \"type\": \"img_25\" }\n" +
                "}\n";

        ObjectMapper mapper=new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(s);
        System.out.println(jsonNode.toString());
        JsonNode path = jsonNode.findPath("1");
        System.out.println("path = " + path);
        System.out.println(path.findValue("m_src"));
        Iterator<JsonNode> elements = jsonNode.elements();
        Iterator<String> fieldNames = jsonNode.fieldNames();
        while (fieldNames.hasNext()) {
            System.out.println(fieldNames.next());
        }
        while (elements.hasNext()) {
            System.out.println(elements.next());
        }
    }
}
